package com.villaday.idf.activity;

import com.squareup.picasso.Picasso;
import com.villaday.idf.R;
import com.villaday.idf.base.BaseActivity;
import com.xiao.nicevideoplayer.NiceVideoPlayer;
import com.xiao.nicevideoplayer.NiceVideoPlayerManager;
import com.xiao.nicevideoplayer.TxVideoPlayerController;

import java.util.HashMap;

import butterknife.BindView;

public class VideoPlayActivity extends BaseActivity {
    @BindView(R.id.nice_video_player)
    NiceVideoPlayer mNiceVideoPlayer;

    @Override
    protected int getViewResId() {
        return R.layout.activity_video_play;
    }

    @Override
    protected void init() {

        getVideoWh(getIntent().getStringExtra("videoUrl"));


    }

    private void getVideoWh(String mUri) {
        android.media.MediaMetadataRetriever mmr = new android.media.MediaMetadataRetriever();
        try {
            if (mUri != null) {
                HashMap<String, String> headers = null;
                if (headers == null) {
                    headers = new HashMap<String, String>();
                    headers.put("User-Agent", "Mozilla/5.0 (Linux; U; Android 4.4.2; zh-CN; MW-KW-001 Build/JRO03C) AppleWebKit/533.1 (KHTML, like Gecko) Version/4.0 UCBrowser/1.0.0.001 U4/0.8.0 Mobile Safari/533.1");
                }
                mmr.setDataSource(mUri, headers);
            } else {
                //mmr.setDataSource(mFD, mOffset, mLength);
            }
            mNiceVideoPlayer = (NiceVideoPlayer) findViewById(R.id.nice_video_player);
            mNiceVideoPlayer.setPlayerType(NiceVideoPlayer.TYPE_IJK); // IjkPlayer or MediaPlayer
            String videoUrl = getIntent().getStringExtra("videoUrl");
//        videoUrl = Environment.getExternalStorageDirectory().getPath().concat("/办公室小野.mp4");
            mNiceVideoPlayer.setUp(videoUrl, null);
            TxVideoPlayerController controller = new TxVideoPlayerController(this);
            controller.setTitle(getIntent().getStringExtra("title"));
            controller.setDes(getIntent().getStringExtra("des"));
            String duration = mmr.extractMetadata(android.media.MediaMetadataRetriever.METADATA_KEY_DURATION);//时长(毫秒)
            String width = mmr.extractMetadata(android.media.MediaMetadataRetriever.METADATA_KEY_VIDEO_WIDTH);//宽
            String height = mmr.extractMetadata(android.media.MediaMetadataRetriever.METADATA_KEY_VIDEO_HEIGHT);//高
            controller.setLenght(Long.parseLong(duration));
            controller.setActivity(this);

            Picasso picasso = new Picasso.Builder(this).loggingEnabled(true).build();
            picasso.load(getIntent().getStringExtra("photoUrl") + "")
                    .placeholder(R.drawable.focus)
                    .error(R.drawable.focus)
                    .into(controller.imageView());
            mNiceVideoPlayer.setController(controller);


            if (Long.parseLong(width) > Long.parseLong(height)) {
                mNiceVideoPlayer.enterFullScreen();
            } else {
            }


            controller.getStartImg().performClick();

        } catch (Exception ex) {
        } finally {
            mmr.release();
        }

    }

    @Override
    protected void onStop() {
        super.onStop();
        NiceVideoPlayerManager.instance().releaseNiceVideoPlayer();
    }

    @Override
    public void onBackPressed() {
        if (NiceVideoPlayerManager.instance().onBackPressd())
            return;
        super.onBackPressed();
    }
}
